package com.permission.controller;

import com.permission.dto.request.user.LoginRequest;
import com.permission.dto.response.user.PicCaptchaResponse;
import com.permission.message.Result;
import com.permission.message.StandardResponse;
import com.permission.service.LoginService;
import com.permission.utils.VerifyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @desc: 登录注册
 * @author: xhs
 * @date: 2021/8/19 12:54
 * @version: JDK 1.8
 */
@Slf4j
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private VerifyUtil captchaUtil;

    /**
     * 注册
     *
     * @param loginRequest
     * @return
     */
    @PostMapping("/register")
    public StandardResponse<Object> register(@Valid @RequestBody LoginRequest loginRequest) {
        return loginService.register(loginRequest);
    }

    /**
     * 生成图形验证码
     *
     * @return
     */
    @GetMapping("/code")
    public StandardResponse<Object> code() {
        PicCaptchaResponse captcha = captchaUtil.getPicCaptcha();
        return StandardResponse.build(Result.SUCCESS).setData(captcha);
    }

    /**
     * 登录
     *
     * @param loginRequest
     * @return
     */
    @PostMapping("/login")
    public StandardResponse<Object> login(@Valid @RequestBody LoginRequest loginRequest, HttpServletRequest request) {
        return loginService.login(request, loginRequest);
    }

    /**
     * 登出
     *
     * @return
     */
    @PostMapping("/logout")
    public StandardResponse<Object> logout() {
        return loginService.logout();
    }
}
